/**
 * Created by xiongxin on 2017/12/10.
 */

import Vue from 'vue'
import axios from 'axios';
import qs from 'qs';
import  mintUi  from 'mint-ui';

// console.log(mintUi);


import kj_url from './kj_url';



Vue.prototype.$axios = axios;
axios.defaults.withCredentials=true

axios.defaults.timeout =  15000;


//添加请求拦截器
axios.interceptors.request.use(
  config => {


    /**
     * config和之前版本的request shi
     */
    config.data = qs.stringify(config.data);
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    config.headers['acton'] =  window.sessionStorage.getItem('token');
    config.headers['userid'] = window.sessionStorage.getItem('userId');
    return config;
  },
  err => {
    return Promise.reject(err);
  });

//添加一个响应拦截器
axios.interceptors.response.use(response => {
    return response;
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:

          var keys=document.cookie.match(/[^ =;]+(?=\=)/g);
          if (keys) {
            for (var i = keys.length; i--;)
              document.cookie=keys[i]+'=0;expires=' + new Date( 0).toUTCString()
          }

          window.location.href= kj_url.bank_login + 'clientid=' +kj_url.clientid+ '&clientSecret='+kj_url.clientSecret+'&isBranches=true&encrypt_type=raw&wpRedirect='+encodeURI(kj_url.indexHref);
          break;
        //window.location.href='https://open.boc.cn/wap/login/dbLogin?clientid=501&clientSecret=07ec5c01ad8106b2745bc29023bd4d1f7a8096dcf64fb52373&isBranches=true&encrypt_type=raw&wpRedirect='+encodeURI('http://localhost:8080/app/index');
        // 这里写清除token的代码
        // router.replace({
        //   //path: 'pay/signed'
        //   //query: {redirect: 'https://open.boc.cn/wap/login/dbLogin?clientid=501&clientSecret=07ec5c01ad8106b2745bc29023bd4d1f7a8096dcf64fb52373&isBranches=true&encrypt_type=raw&wpRedirect='+encodeURI('http://localhost:8080/app/index')}   //登录成功后跳入浏览的当前页面
        // })

        // case 408:
        //   alert('服务器请求超时'+ err.message);
        //   alert('服务器请求超时'+ error.response);

      }
    }
    return Promise.reject(error.response.data)
  });

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch(url,params={}){
  return new Promise((resolve,reject) => {
    loadPop.show();
    axios.get(url,{
        params:params
      })
      .then(response => {
        loadPop.hide();
        resolve(response.data);
      })
      .catch(err => {
        loadPop.hide();
        reject(err)
      })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url,data = {}){
  return new Promise((resolve,reject) => {
    loadPop.show();
    axios.post(url,data)
      .then(response => {
        loadPop.hide();
        resolve(response.data);


        // loadPop.hide();
        // if(response.status == 'success'){
        //   resolve(response.data);
        // } else {
        //   alert('报错啊')
        // }

      },err => {
        loadPop.hide();
        console.log(123123);
        mintUi.MessageBox.confirm('网速太慢，神马都加载不出来').then(function (action) {
          // alert(action+'nimeia !!')
          window.location.reload()
        });
        // console.log(err);
        reject(err)
      })
  })
}
